package com.eastfair.venuebooking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.BelongRecordMapper;
import com.eastfair.venuebooking.dto.BelongToAssignDTO;
import com.eastfair.venuebooking.entity.BelongRecord;
import com.eastfair.venuebooking.enums.VenueBookingMoudleEnum;
import com.eastfair.venuebooking.service.BelongRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * <p>
 * 业务实现类
 * 客户归属记录表
 * </p>
 *
 * @author clm
 * @date 2022-06-10
 */
@Slf4j
@Service
public class BelongRecordServiceImpl extends SuperServiceImpl<BelongRecordMapper, BelongRecord> implements BelongRecordService {

    @Resource
    private UserAccountServiceFeign userAccountServiceFeign;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<BelongRecord> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(BelongRecord model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setTenantId(ContextUtil.getTenantId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBelongRecord(BelongToAssignDTO assignDTO) {
        BelongRecord belongRecordSaveDTO = new BelongRecord();
        belongRecordSaveDTO.setNowBelongId(assignDTO.getBelongTo());
        belongRecordSaveDTO.setNowBelongName(assignDTO.getBelongToName());
        belongRecordSaveDTO.setPastBelongId(assignDTO.getLastBelongTo());
        belongRecordSaveDTO.setPastBelongName(assignDTO.getLastBelongToName());
        belongRecordSaveDTO.setBusinessId(assignDTO.getId());
        belongRecordSaveDTO.setBusinessType(assignDTO.getBusinessType());
        belongRecordSaveDTO.setOperaterId(ContextUtil.getUserId());
        belongRecordSaveDTO.setOperateType(assignDTO.getOperateType().getCode());

        R<String> r = userAccountServiceFeign.getName(ContextUtil.getUserId());
        String operaterName = r.getData();
        belongRecordSaveDTO.setOperaterName(operaterName);
        belongRecordSaveDTO.setRemark(assignDTO.getRemark());
        return save(belongRecordSaveDTO);
    }

    @Override
    public long countBelongRecordByCustomerId(Long customerId) {
        log.info("countBelongRecordByCustomerId - 客户ID统计归属记录数量, customerId={}", customerId);

        QueryWrapper<BelongRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BelongRecord::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(customerId != null, BelongRecord::getBusinessId, customerId)
                .eq(BelongRecord::getBusinessType, VenueBookingMoudleEnum.CUSTOMER.getCode());
        return count(queryWrapper);
    }
}
